package com.qtjf.qtmoneyservice.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.FirstShenFenBean;
import com.qtjf.qtmoneyservice.domain.ResultBanner;
import com.qtjf.qtmoneyservice.utils.RequestNetApi;
import com.qtjf.qtmoneyservice.view.HomeRecyclerHeadView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import impl.IHttpListener;

public class FirstRenZhengActivity extends BaseActivity {

    ImageView mBack;
    EditText nameText;
    EditText idCardText;
    TextView submitBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_ren_zheng);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mBack= (ImageView) findViewById(R.id.back_bt);
        nameText= (EditText) findViewById(R.id.name_text);
        idCardText= (EditText) findViewById(R.id.idcard_text);
        submitBt= (TextView) findViewById(R.id.submit_bt);
    }

    private void initData() {
    }

    private void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirstRenZhengActivity.this.finish();
            }
        });

        submitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                request(nameText.getText().toString().trim(),idCardText.getText().toString().trim());
            }
        });
    }

    private void request(String name, final String idcard) {
        final String xingMing=name;
        //http://118.89.185.176:8082/juhe/idCardAuth?idcard=3302041XXXXXXXXXXXX&realname=泷泽萝拉
        try {
            name = URLEncoder.encode(name, "utf-8"); //先对中文进行UTF-8编码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        final String url=Constant.HOMEADDRESS + "juhe/idCardAuth?idcard="+idcard+"&realname="+name;
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Gson gson=new Gson();
                if(response.length()>5) {
                    FirstShenFenBean firstShenFenBean = gson.fromJson(response, new TypeToken<FirstShenFenBean>() {
                    }.getType());

                    if(firstShenFenBean.getResult()!=null&&firstShenFenBean.getResult().getRes()==1)
                    {
                       // Toast.makeText(FirstRenZhengActivity.this, "匹配成功", Toast.LENGTH_SHORT).show();
                        request2(xingMing,idcard);
                    }else
                    {
                        Toast.makeText(FirstRenZhengActivity.this, "身份信息不匹配", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(FirstRenZhengActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        })
            {//设置get请求的头，编码格式也为UTF-8
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Charset", "utf-8");
                headers.put("Content-Type", "application/x-javascript");
                headers.put("Accept-Encoding", "gzip,deflate");
                return headers;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }


    private void request2(String name,String idcard) {
        //http://118.89.185.176:8082/users/sumbitVerifyCrad?Mobile=123&idcrad=23&realName=sss
        try {
            name = URLEncoder.encode(name, "utf-8"); //先对中文进行UTF-8编码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        final String url=Constant.HOMEADDRESS + "users/sumbitVerifyCrad?Mobile="+Constant.getUsermobile()+"&idcrad="+idcard+"&realName="+name;

        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Gson gson=new Gson();
                if(response.contains("200")) {
                    Toast.makeText(FirstRenZhengActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                    Constant.setShiMingState("1");
                    FirstRenZhengActivity.this.finish();
                }
               /* if(response.length()>5) {
                    FirstShenFenBean firstShenFenBean = gson.fromJson(response, new TypeToken<FirstShenFenBean>() {
                    }.getType());

                    if(firstShenFenBean.getResult()!=null&&firstShenFenBean.getResult().getRes()==1)
                    {
                        Toast.makeText(FirstRenZhengActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                        Constant.setShiMingState("1");
                        FirstRenZhengActivity.this.finish();
                    }else
                    {
                        Toast.makeText(FirstRenZhengActivity.this, "身份信息不匹配", Toast.LENGTH_SHORT).show();
                    }

                }*/
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(FirstRenZhengActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        })
        {//设置get请求的头，编码格式也为UTF-8
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Charset", "utf-8");
                headers.put("Content-Type", "application/x-javascript");
                headers.put("Accept-Encoding", "gzip,deflate");
                return headers;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }

}
